package com.jielin.handling.controller.room;

import com.alibaba.fastjson.JSONObject;
import com.jielin.handling.Annotation.MyPower;
import com.jielin.handling.bean.AppResult;
import com.jielin.handling.entity.room.RoomCameraInfoVo;
import com.jielin.handling.entity.room.RoomInformation;
import com.jielin.handling.entity.room.RoomRecord;
import com.jielin.handling.exception.ResultEnum;
import com.jielin.handling.service.room.RoomInformationService;
import com.jielin.handling.service.room.RoomRecordService;
import com.jielin.handling.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author WIN10 分配审讯室
 */
@RestController
@RequestMapping("/room")
@Slf4j
@CrossOrigin
@MyPower(name = "分配审讯室", url = "/room")
public class RoomController {
    @Autowired
    private RoomInformationService roomInformationService;
    @Autowired
    private RoomRecordService roomRecordService;

    /**
     * 查询所有房间的状态
     *
     * @return
     */
    @MyPower(name = "查询所有房间的状态", url = "/findAllRoomStatus")
    @RequestMapping(value = "/findAllRoomStatus")
    public AppResult findAllRoomStatus(String organizationId,
                                       @RequestParam(name = "pageNum", defaultValue = "1") int pageNum,
                                       @RequestParam(name = "pageSize", defaultValue = "8") int pageSize) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        log.info("..........findAllRoomStatus(查询所有房间的状态)");
        List<RoomInformation> list = roomInformationService.findAllRoomStatus(organizationId, pageNum, pageSize);
        data.put("value", list);
        result.setCode(200);
        result.setMsg("查询成功！！");
        result.setData(data);
        return result;
    }

    /**
     * 查询本单位房间区域信息与摄像头信息(实时视频监督)
     *
     * @return
     */
    @MyPower(name = "查询本单位房间区域信息与摄像头信息(实时视频监督)", url = "/findRoomCameraInfoByOrganizationId")
    @RequestMapping("/findRoomCameraInfoByOrganizationId")
    public AppResult findRoomCameraInfoByOrganizationId(String organizationId) {
        log.info("查询单位信息===organizationId:" + organizationId);
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        List<RoomCameraInfoVo> list = roomInformationService.findRoomCameraInfoByOrganizationId(organizationId);
        data.put("value", list);
        result.setCode(200);
        result.setMsg("根据单位编号查询所有摄像头信息成功！");
        result.setData(data);
        return result;
    }

    /**
     * 查询房间的摄像头信息
     *
     * @return
     */
    @MyPower(name = "查询房间的摄像头信息", url = "/findRoomCameraInfoByRoomId")
    @RequestMapping("/findRoomCameraInfoByRoomId")
    public AppResult findRoomCameraInfoByRoomId(String roomId) {
        log.info("查询房间的摄像头信息===roomId:" + roomId);
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        RoomCameraInfoVo rciv = roomInformationService.findRoomCameraInfoByRoomId(roomId);
        data.put("value", rciv);
        result.setCode(200);
        result.setMsg("查询房间的摄像头信息成功！");
        result.setData(data);
        return result;
    }

    /**
     * 预约空闲房间
     *
     * @param roomId    房间id
     * @param policeId  警员id
     * @param suspectId 嫌疑人id
     * @param orgId     单位id
     * @param method    预约方式={0:电脑;1:手机;默认0}
     * @param startDate 预约开始时间
     * @param endDate   预约结束时间
     * @return 新增的房间使用记录的id
     */
    @MyPower(name = "预约空闲房间", url = "/reserveRoom")
    @RequestMapping(value = "/reserveRoom")
    public AppResult reserveRoom(String roomId, String policeId, String suspectId, String orgId,
                                 @RequestParam(name = "method", defaultValue = "0") int method, String startDate, String endDate) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        log.info("..........ReserveRoom(预约空闲房间)");
        String roomrecordid = roomInformationService.reserveRoom(roomId, policeId, suspectId, orgId, method, startDate,
                endDate);
        if ("-1".equals(roomrecordid)) {
            data.put("value", roomrecordid);
            result.setCode(-1);
            result.setMsg("预约空闲房间失败！（该房间已有人或已有人预约）");
            result.setData(data);
        } else if ("-2".equals(roomrecordid)) {
            data.put("value", roomrecordid);
            result.setCode(-1);
            result.setMsg("预约空闲房间失败！（已经预约过房间了）");
            result.setData(data);
        } else {
            data.put("value", roomrecordid);
            result.setCode(200);
            result.setMsg("预约空闲房间成功！");
            result.setData(data);
        }
        return result;
    }

    /**
     * 取消预约房间
     *
     * @param roomid       房间id
     * @param roomrecordid 房间使用记录的id
     * @param policeId     警员id
     * @return
     */
    @MyPower(name = "取消预约房间", url = "/cancelreserveRoom")
    @RequestMapping(value = "/cancelreserveRoom")
    public AppResult cancelreserveRoom(String roomid, String roomrecordid, String policeId) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        log.info("..........cancelreserveRoom(取消预约房间)");
        int i = roomInformationService.cancelreserveRoom(roomid, roomrecordid, policeId);
        if (i == -1) {
            data.put("value", i);
            result.setCode(-1);
            result.setMsg("取消预约房间失败！（可能原因：没有传入policeId）");
            result.setData(data);
        } else if (i == -2) {
            data.put("value", i);
            result.setCode(-1);
            result.setMsg("取消预约房间失败！（只能取消自己预约的房间）");
            result.setData(data);
        } else {
            data.put("value", i);
            result.setCode(200);
            result.setMsg("取消预约房间成功！");
            result.setData(data);
        }
        return result;
    }

    /**
     * 使用预约的房间
     *
     * @param roomid 房间id
     * @return
     */
    @MyPower(name = "使用预约的房间", url = "/useStartReserveRoom")
    @RequestMapping(value = "/useStartReserveRoom")
    public AppResult useStartReserveRoom(String roomid) {
        JSONObject data = new JSONObject();
        if (StringUtil.isEmpty(roomid)) {
            return AppResult.builder().setCode(ResultEnum.FAIL.getCode()).setMsg("没有预约房间！");
        }
        data.put("value", roomInformationService.useStartReserveRoom(roomid));
        return AppResult.builder().setCode(ResultEnum.SUCCESS.getCode()).setMsg("使用预约的房间成功！").setData(data);
    }

    /**
     * 使用完房间，改为房间状态为空闲 添加房间使用结束记录
     *
     * @param roomid       房间id
     * @param roomrecordid 房间使用记录的id
     * @param leaveallcase 离开原因
     * @return
     */
    @MyPower(name = "使用完房间，改为房间状态为空闲，添加房间使用结束记录", url = "/useEndReserveRoom")
    @RequestMapping(value = "/useEndReserveRoom")
    public AppResult useEndReserveRoom(String roomid, String roomrecordid, String leaveallcase) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        log.info("..........useEndReserveRoom(使用完房间，改为房间状态为空闲)");
        Integer i = roomInformationService.useEndReserveRoom(roomid, roomrecordid, leaveallcase);
        data.put("value", i);
        result.setCode(200);
        result.setMsg("使用完房间，改为房间状态为空闲成功！");
        result.setData(data);
        return result;
    }

    /**
     * 根据嫌疑人id查询该嫌疑人的活动记录
     *
     * @param suspectid 嫌疑人id
     * @param leaveType 类型(0:人员活动1:临时离开2:正常房间使用记录)
     * @return
     */
    @MyPower(name = "根据嫌疑人id查询该嫌疑人的活动记录", url = "/findSuspectRecord")
    @RequestMapping(value = "/findSuspectRecord")
    public AppResult findSuspectRecord(String suspectid, String leaveType) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        log.info("..........findSuspectRecord(根据嫌疑人id查询该嫌疑人的活动记录)");
        List<RoomRecord> list = roomRecordService.findSuspectRecord(suspectid, leaveType);
        data.put("value", list);
        result.setCode(200);
        result.setMsg("查询成功！！");
        result.setData(data);
        return result;
    }

    /**
     * 添加嫌疑人的活动记录
     *
     * @param policeid  陪同警员id
     * @param suspectid 嫌疑人id
     * @return roomrecordid 房间使用记录的id
     */
    @MyPower(name = "添加嫌疑人的活动记录", url = "/addSuspectRecord")
    @RequestMapping(value = "/addSuspectRecord")
    public AppResult addSuspectRecord(@RequestBody RoomRecord roomRecord) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        log.info("..........addSuspectRecord(添加嫌疑人的活动记录)");
        String roomrecordid = roomRecordService.addSuspectRecord(roomRecord);
        data.put("roomrecordid", roomrecordid);
        result.setCode(200);
        result.setMsg("添加嫌疑人的活动记录成功！");
        result.setData(data);
        return result;
    }

    /**
     * 更新嫌疑人返回的活动记录
     *
     * @param roomrecordid 房间使用记录的id
     * @return
     */
    @MyPower(name = "更新嫌疑人返回的活动记录", url = "/addEndSuspectRecord")
    @RequestMapping(value = "/addEndSuspectRecord")
    public AppResult addEndSuspectRecord(String roomrecordid, String leaveallcase) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        log.info("..........addEndSuspectRecord(更新嫌疑人返回的活动记录)");
        data.put("value", roomRecordService.addEndSuspectRecord(leaveallcase, roomrecordid));
        result.setCode(200);
        result.setMsg("更新嫌疑人返回的活动记录成功！");
        result.setData(data);
        return result;
    }

    /**
     * 更新嫌疑人返回的活动记录
     *
     * @param roomrecordid 使用记录的id
     * @return
     */
    @MyPower(name = "更新嫌疑人返回的活动记录", url = "/addEndSuspectRecordTwo")
    @RequestMapping(value = "/addEndSuspectRecordTwo")
    public AppResult addEndSuspectRecordTwo(String roomrecordid) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        log.info("..........addEndSuspectRecord(更新嫌疑人返回的活动记录)");
        data.put("value", roomRecordService.addEndSuspectRecord(roomrecordid));
        result.setCode(200);
        result.setMsg("更新嫌疑人返回的活动记录成功！");
        result.setData(data);
        return result;
    }

    /**
     * 查询最近一次的房间使用记录
     *
     * @param roomId    房间id
     * @param policeId  警员id
     * @param leaveType 类型(0:人员活动1:临时离开2:正常房间使用记录)
     * @return
     */
    @MyPower(name = "查询最近一次的房间使用记录", url = "/findLastRommRecordByRoomId", isPublic = true)
    @RequestMapping(value = "/findLastRommRecordByRoomId")
    public AppResult findLastRommRecordByRoomId(String roomId, String policeId) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        log.info("..........findLastRommRecordByRoomId(根据房间id查询最近一次的房间使用记录)");
        RoomRecord r = roomRecordService.findLastRommRecordByRoomId(roomId, policeId);
        data.put("value", r);
        result.setCode(200);
        result.setMsg("查询成功！");
        result.setData(data);
        return result;
    }
}
